package com.example.mydemoapplication;

import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.mydemoapplication.presenter.DownloadPresenterCompl;
import com.example.mydemoapplication.view.IDownloadView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity  implements IDownloadView, View.OnClickListener{
    private Button btn_countdownTime;
    protected TextView tv_countdowntime;
    private ProgressBar progressBar;
    private DownloadPresenterCompl downloadPresenterCompl;
    private boolean downloadStarted = false;
    public String TAG = "MyDemoApplication:MainActivity";
    private int status = 0; // 0:未下载， 1：下载中 2：完成下载

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        Log.d(TAG, "before init downloadPresenterCompl:" + downloadPresenterCompl);
        downloadPresenterCompl = new DownloadPresenterCompl(this);
        Log.d(TAG, "after init downloadPresenterCompl:" + downloadPresenterCompl);

    }
    private void findViews() {
        btn_countdownTime = (Button)findViewById( R.id.btn_countdownTime );
        tv_countdowntime = findViewById( R.id.tv_countdowntime );
        progressBar = findViewById(R.id.progressBar);

        btn_countdownTime.setOnClickListener( this );
    }

    @Override
    public void onUpdateDownloadButton(String status, int key){
        this.status = key;
        btn_countdownTime.setText(status);
        Toast.makeText(getApplicationContext(), status, Toast.LENGTH_LONG).show();
    }


    @Override
    public void onUpdateProgress(int val){
        tv_countdowntime.setText(String.format("Process:%d%%",val*10));
        progressBar.setProgress(val*10);

    }

    /**
     * Handle button click events<br />
     * <br />
     * Auto-created on 2020-05-14 09:37:16 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    @Override
    public void onClick(View v) {

        Log.d(TAG, " in onClick downloadPresenterCompl:" + downloadPresenterCompl);
        if(v == btn_countdownTime){
            if(status == 0 || status == 2){
                downloadPresenterCompl.startDownload();

            }else{
                downloadPresenterCompl.stopDownload();
            }
        }
    }
}